﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
using URIDecoder;
namespace ConfigWindows
{
    public partial class SchemeControl : UserControl
    {
        public EncodedURIScheme Scheme { get; set; }
        public string SchemeHandler { get; set; }
        public SchemeControl(EncodedURIScheme scheme)
        {
            this.Scheme = scheme;
            SchemeHandler = Registry.GetValue(string.Format(@"HKEY_CLASSES_ROOT\{0}\shell\open\command", scheme.Scheme), null, "") as string;
            InitializeComponent();
            this.groupBox1.Text = (textBox5.Text = Scheme.Scheme) + "://";
            this.textBox1.Text = SchemeHandler;
            this.textBox2.Text = Scheme.EncodedUriRegex;
            this.textBox3.Text = Scheme.OriginalUriRegex;
            this.textBox6.Text = Scheme.Name;
            switch (Scheme.EncodeMethod)
            {
                case "base64":
                    this.radioButton2.Checked = true;
                    break;
                case "uriescape":
                    this.radioButton3.Checked = true;
                    break;
                case "plaintext":
                case "":
                    this.radioButton1.Checked = true;
                    break;
                default:
                    this.radioButton4.Checked = true;
                    break;
            }
            this.textBox4.Text = Scheme.EncodeMethod;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format(
@"要使用外部扩展，请将扩展DLL文件放置在与程序相同目录下。
该DLL必须为.net 组件，且必须包含一个实现接口 URIDecoder.IURIDecoder 的类。
保存的文件名格式应为 扩展名称.dll 。
要使用该扩展，请在协议处理的编码规则中选择“使用外部扩展”，并填入扩展名称（不含.dll后缀）。"
));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
@"当程序试图解码此协议的URI时，会使用两个正则表达式。

程序首先使用“要解密部分的正则”来对未解密的URI进行匹配。
如果匹配成功，程序将对正则表达式中编号为1的组的内容进行解密。

解密后，程序会使用“解密后URI的正则”来对解密后的内容进行匹配。
如果匹配成功，程序会认为正则表达式中编号为1的组的内容为实际下载地址，并调用程序进行下载。
"
);
        }

        public void SetHandler(object sender, EventArgs e)
        {
            textBox1.Text = "\"" + Program.URIDecoderFileName + "\" \"%1\"";
        }
        public void Save()
        {
            SchemeHandler = this.textBox1.Text;
            Scheme.EncodedUriRegex = this.textBox2.Text;
            Scheme.OriginalUriRegex = this.textBox3.Text;
            Scheme.Name = this.textBox6.Text;
            Scheme.Scheme = this.textBox5.Text;
            if (radioButton1.Checked)
                Scheme.EncodeMethod = radioButton1.Tag as string;
            if (radioButton2.Checked)
                Scheme.EncodeMethod = radioButton2.Tag as string;
            if (radioButton3.Checked)
                Scheme.EncodeMethod = radioButton3.Tag as string;
            if (radioButton4.Checked)
                Scheme.EncodeMethod = textBox4.Text;
            Registry.SetValue(string.Format(@"HKEY_CLASSES_ROOT\{0}\shell\open\command", Scheme.Scheme), null, SchemeHandler);
            string key = string.Format(@"HKEY_CLASSES_ROOT\{0}\DefaultIcon", Scheme.Scheme);
            if (Registry.GetValue(key, null, null) == null)
                Registry.SetValue(key, null, string.Format("\"{0}\",1", Program.URIDecoderFileName));
            key = string.Format(@"HKEY_CLASSES_ROOT\{0}", Scheme.Scheme);
            if (Registry.GetValue(key, null, null) == null)
                Registry.SetValue(key, null, Scheme.Name);
            if (Registry.GetValue(key, "URL Protocol", null) == null)
                Registry.SetValue(key, "URL Protocol", "");
        }
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            this.groupBox1.Text = (Scheme.Scheme = textBox5.Text) + "://";
        }

        public event EventHandler Deleted;
        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("删除协议后，程序会解除注册表中此程序对协议的关联，而且程序将无法处理此种协议。确定要删除吗？", "删除协议", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Registry.SetValue(string.Format(@"HKEY_CLASSES_ROOT\{0}\shell\open\command", Scheme.Scheme), null, string.Empty);
                Deleted(this, EventArgs.Empty);
            }

        }
    }
}
